Home
>
Archives
>
微服务:Eureka和Discovery Client
微服务:Eureka和Discovery Client
Publish: August 13, 2019
1
2
3
4
5
6
7
8
9
@EnableEurekaServer
@SpringBootApplication
public class EurekaApplication {
public static void main( String[] args) {
SpringApplication.run( EurekaApplication.class, args) ;
}
}
在application.properties中添加配置
1
2
3
4
server.port= 1111
eureka.client.register-with-eureka= false
eureka.client.fetch-registry= false
eureka.client.serviceUrl.defaultZone= http://localhost:${ server .port } /eureka
地址栏访问 http://localhost:1111/
1
2
3
4
5
6
7
8
9
10
@EnableDiscoveryClient
@SpringBootApplication
public class ComputeApplication {
public static void main( String[] args) {
SpringApplication.run( ComputeApplication.class, args) ;
}
}
在application.properties中添加配置
1
2
3
spring.application.name= compute-service
server.port= 2223
eureka.client.serviceUrl.defaultZone= http://localhost:1111/eureka/
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import org.slf4j.Logger ;
import org.slf4j.LoggerFactory ;
import org.springframework.web.bind.annotation.RequestMapping ;
import org.springframework.web.bind.annotation.RequestMethod ;
import org.springframework.web.bind.annotation.RequestParam ;
import org.springframework.web.bind.annotation.RestController ;
@RestController
public class ComputeController {
Logger logger = LoggerFactory . getLogger ( ComputeController . class );
@RequestMapping ( value = "/add" , method = RequestMethod . GET )
public Integer add ( @RequestParam Integer a , @RequestParam Integer b ) {
Integer r = a + b ;
logger . info ( "/add, result:" + r );
return r ;
}
}
访问 http://localhost:2223/add?a=1&b=2
参考
声明: 本文采用 BY-NC-SA 授权。转载请注明转自: Ding Bao Guo