标签:方案 添加 art 实体 run template register obj spi
1
.创建maven工程
2.添加web和eureka依赖
在pom.xml添加依赖如下,目的是@DATA用到
<!-- lombok代码模板解决方案,没有使用lombok的可以把这个依赖删除 -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.10</version>
</dependency>
3.编写服务端口和注册配置application.yml
server:
port: 8761
spring:
application:
name: eureka-server1
eureka:
instance:
hostname: server1
client:
registerWithEureka: false
fetchRegistry : false
serviceUrl:
defaultZone: http://server2:8762/eureka/
4.编写启动类EurekaServerApplication.java,添加@EnableEurekaServer
@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaServerApplication.class, args);
}
}
4-2启动
5.用统样方法配置端口8762的服务注册,和8761互相注册调用
6.编写消费者consumer,
用同样方法创建springboot的项目,
配置application.yml如下
server:
port: 8084
spring:
application:
name: eureka-consumer
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka,http://localhost:8762/eureka
编写启动类如下:
@SpringBootApplication
@EnableEurekaClient
public class MicreoserviceDicoveryEurekaConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(MicreoserviceDicoveryEurekaConsumerApplication.class, args);
}
}
编写控制层类如下
@Controller
@Configuration
public class ConsumerController {
@Bean
@LoadBalanced
public RestTemplate getRestTemplate(){
return new RestTemplate();
}
//当访问localhost:8084/router时跳转到http://eureka-provider/search/1地址
@GetMapping(value = "/router")
@ResponseBody
public String router() {
RestTemplate temp = getRestTemplate();
return temp.getForObject("http://eureka-provider/search/1", String.class);
}
}
7.用同样方法创建springboot的供应商类
application.yml如下
spring:
application:
name: eureka-provider
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka,http://localhost:8762/eureka
server:
port: 8082
启动类如下:
@SpringBootApplication
@EnableEurekaClient
public class MicreaoserviceDicoveryEurekaProvider02Application {
public static void main (String[]args){
SpringApplication.run(MicreaoserviceDicoveryEurekaProvider02Application.class, args);
}
}
控制层类如下
@RestController
public class ProviderController {
@RequestMapping(value = "/search/{id}", method = RequestMethod.GET,produces = MediaType.APPLICATION_JSON_VALUE)
@ResponseBody
public Person searchPerson(@PathVariable Integer id, HttpServletRequest request) {
Person person = new Person();
person.setId(id);
person.setName("Spirit");
person.setMessage(request.getRequestURL().toString());
return person;
}
}
实体类如下
@Data
public class Person {
private Integer id; //主键ID
private String name; //姓名
private String message; //信息
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
}
8.先启动8761和8762端口,然后访问http://localhost:8762/和http://localhost:8761/可以看到注册的端口
9.再启动8082和8084端口,然后访问localhost:8084/router会跳转到http://eureka-provider/search/1页面
标签:方案 添加 art 实体 run template register obj spi
原文地址:https://www.cnblogs.com/laivieeeee/p/10887978.html