1
.创建maven工程
2.添加web和eureka依赖
在pom.xml添加依赖如下,目的是@DATA用到
<!-- lombok代码模板解决方案,没有使用lombok的可以把这个依赖删除 -->@H_404_20@<dependency>@H_404_20@ <groupID>org.projectlombok</groupID>@H_404_20@ <artifactID>lombok</artifactID>@H_404_20@ <version>1.16.10</version>@H_404_20@</dependency>
3.编写服务端口和注册配置application.yml
server:@H_404_20@ port: 8761@H_404_20@spring:@H_404_20@ application:@H_404_20@ name: eureka-server1@H_404_20@eureka:@H_404_20@ instance:@H_404_20@ hostname: server1@H_404_20@ clIEnt:@H_404_20@ registerWithEureka: false@H_404_20@ fetchRegistry : false@H_404_20@ serviceUrl:@H_404_20@ defaultZone: http://server2:8762/eureka/@H_404_20@4.编写启动类EurekaServerApplication.java,添加@EnableEurekaServer
@SpringBootApplication@H_404_20@@EnableEurekaServer@H_404_20@public class EurekaServerApplication {@H_404_20@@H_404_20@ public static voID main(String[] args) {@H_404_20@ SpringApplication.run(EurekaServerApplication.class,args);@H_404_20@ }@H_404_20@@H_404_20@}@H_404_20@4-2启动
5.用统样方法配置端口8762的服务注册,和8761互相注册调用
6.编写消费者consumer,
用同样方法创建springboot的项目,
配置application.yml如下
server:@H_404_20@ port: 8084@H_404_20@@H_404_20@spring:@H_404_20@ application:@H_404_20@ name: eureka-consumer@H_404_20@@H_404_20@eureka:@H_404_20@ clIEnt:@H_404_20@ serviceUrl:@H_404_20@ defaultZone: http://localhost:8761/eureka,http://localhost:8762/eureka@H_404_20@编写启动类如下:
@SpringBootApplication@H_404_20@@EnableEurekaClIEnt@H_404_20@public class MicreoserviceDicoveryEurekaConsumerApplication {@H_404_20@@H_404_20@ public static voID main(String[] args) {@H_404_20@ SpringApplication.run(MicreoserviceDicoveryEurekaConsumerApplication.class,args);@H_404_20@ }@H_404_20@@H_404_20@}@H_404_20@编写控制层类如下
@Controller@H_404_20@@Configuration@H_404_20@public class ConsumerController {@H_404_20@ @Bean@H_404_20@ @LoadBalanced@H_404_20@ public RestTemplate getRestTemplate(){@H_404_20@ return new RestTemplate();@H_404_20@ }@H_404_20@//当访问localhost:8084/router时跳转到http://eureka-provIDer/search/1地址
@GetMapPing(value = "/router")@H_404_20@ @ResponseBody@H_404_20@ public String router() {@H_404_20@ RestTemplate temp = getRestTemplate();@H_404_20@ return temp.getForObject("http://eureka-provIDer/search/1",String.class);@H_404_20@ }@H_404_20@}
@H_404_20@7.用同样方法创建springboot的供应商类@H_404_20@application.yml如下
spring:@H_404_20@ application:@H_404_20@ name: eureka-provIDer@H_404_20@@H_404_20@eureka:@H_404_20@ clIEnt:@H_404_20@ serviceUrl:@H_404_20@ defaultZone: http://localhost:8761/eureka,http://localhost:8762/eureka@H_404_20@server:@H_404_20@ port: 8082@H_404_20@启动类如下:
@SpringBootApplication@H_404_20@@EnableEurekaClIEnt@H_404_20@public class MicreaoserviceDicoveryEurekaProvIDer02Application {@H_404_20@@H_404_20@ public static voID main (String[]args){@H_404_20@ SpringApplication.run(MicreaoserviceDicoveryEurekaProvIDer02Application.class,args);@H_404_20@ }@H_404_20@@H_404_20@}@H_404_20@控制层类如下
@RestController@H_404_20@public class ProvIDerController {@H_404_20@@H_404_20@ @RequestMapPing(value = "/search/{ID}",method = RequestMethod.GET,produces = MediaType.APPliCATION_JsON_VALUE)@H_404_20@ @ResponseBody@H_404_20@ public Person searchPerson(@PathVariable Integer ID,httpServletRequest request) {@H_404_20@ Person person = new Person();@H_404_20@ person.setID(ID);@H_404_20@ person.setname("Spirit");@H_404_20@ person.setMessage(request.getRequestURL().toString());@H_404_20@ return person;@H_404_20@@H_404_20@ }@H_404_20@}
实体类如下
@H_404_20@@Data@H_404_20@public class Person {@H_404_20@@H_404_20@ private Integer ID; //主键ID@H_404_20@ private String name; //姓名@H_404_20@ private String message; //信息@H_404_20@@H_404_20@ public Integer getID() {@H_404_20@ return ID;@H_404_20@ }@H_404_20@@H_404_20@ public voID setID(Integer ID) {@H_404_20@ this.ID = ID;@H_404_20@ }@H_404_20@@H_404_20@ public String getname() {@H_404_20@ return name;@H_404_20@ }@H_404_20@@H_404_20@ public voID setname(String name) {@H_404_20@ this.name = name;@H_404_20@ }@H_404_20@@H_404_20@ public String getMessage() {@H_404_20@ return message;@H_404_20@ }@H_404_20@@H_404_20@ public voID setMessage(String message) {@H_404_20@ this.message = message;@H_404_20@ }@H_404_20@}
8.先启动8761和8762端口,然后访问http://localhost:8762/和http://localhost:8761/可以看到注册的端口
9.再启动8082和8084端口,然后访问localhost:8084/router会跳转到http://eureka-provIDer/search/1页面
总结以上是内存溢出为你收集整理的eureka的简单用法全部内容,希望文章能够帮你解决eureka的简单用法所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)