我刚开始尝试在两个微服务之间进行沟通。我创建了四个项目ProjectOne
,ProjectTwo
,ProjectZuul
,ProjectEureka
ProjectOne,ProjectTwo - 微服务 ProjectZuul - Zuul Api网关 ProjectEureka - Eureka Server
我正在尝试从ProjectOne
到ProjectTwo
调用aget方法,但始终显示java.net.UnknownHostException projecttwo
ProjectOne文件
application.properties
#for refering in zuul
spring.application.name = projectone
#database configuration
spring.datasource.url= jdbc:postgresql://192.168.2.6:5432/cliff_test
spring.datasource.username=postgres
spring.datasource.password=password
spring.jpa.hibernate.ddl-auto=create-drop
#Eurekka server configuration
#eurekka client configuration
eureka.client.register-with-eureka = true
eureka.client.fetch-registry = true
// my port
server.port =4444
applicaion.yml
eureka:
client:
serviceUrl:
defaultZone: http://localhost:9999/eureka/
TestController.java
@RestController
public class TestController {
@Autowired
UserRepository mRepository;
@Autowired
private RestTemplate mRestTemplate;
@GetMapping(value = "/test/{username}/{lastname}")
public String create(@PathVariable String username,@PathVariable String lastname){
UserModel mModel = new UserModel(username,lastname);
mRepository.save(mModel);
String dataFromSecond = mRestTemplate.getForObject("http://projecttwo/test",String.class);
// String dataFromSecond = mRestTemplate.getForObject("http://projecttwo/test",String.class);
System.out.println("Data from :"+dataFromSecond);
return "hello world";
}
}
MainClass.Java
@EnableAutoConfiguration
@SpringBootApplication
@EnableEurekaClient
public class ProjectOneApplication {
public static void main(String[] args) {
SpringApplication.run(ProjectOneApplication.class, args);
}
@Bean
public RestTemplate restTemplate() {
return new RestTemplate();
}
}
ProjectTwo文件
application.properties
spring.application.name = projecttwo
server.port = 2222
spring.datasource.url= jdbc:postgresql://192.168.2.6:5432/cliff_test
spring.datasource.username=postgres
spring.datasource.password=password
spring.jpa.hibernate.ddl-auto=create-drop
#eurekka client configuration
eureka.client.register-with-eureka = true
eureka.client.fetch-registry = true
application.yml
eureka:
client:
serviceUrl:
defaultZone: http://localhost:9999/eureka/
TestController.java
@RestController
public class TestController {
@GetMapping(value = "/test")
public String test(){
System.out.println("Hellow call arrived ............................");
return "Second";
}
}
ProjectZuul档案
application.properties
eureka:
client:
serviceUrl:
defaultZone: http://localhost:9999/eureka/
zuul:
prefix: /api
routes:
projectoneservice:
path: /projectone/**
serviceId: projectone
zuulservice:
path: /projectzuul/**
serviceId: projectzuul
projecttwoservice:
path: /projecttwo/**
serviceId: projecttwo
Eureka Server
感谢您的帮助。
答案 0 :(得分:1)
我确定了我的问题并解决了
我的应用程序问题是ProjectOne
主类中的bean启动。
我在Bean initilization中添加了@LoadBalanced
注释。
@EnableAutoConfiguration
@SpringBootApplication
@EnableEurekaClient
public class ProjectOneApplication {
public static void main(String[] args) {
SpringApplication.run(ProjectOneApplication.class, args);
}
@LoadBalanced //adding this line solved the issue
@Bean
public RestTemplate restTemplate() {
return new RestTemplate();
}
}