资讯专栏INFORMATION COLUMN

SpringCloud学习(1)

harryhappy / 829人阅读

摘要:搭建基础服务消费者,去上生成基本的项目骨架,选取需要的包会自动添加到,填写,分别生成,和两个骨架用导入项目,在下新建目录和文件如下,实体可以自己写建个的表对应即可,可以用或者,这里用文件如下这是的实体文件又添加了,和

    搭建Spring cloud 基础服务消费者demo,去spring initializr 上生成基本的Spring boot项目骨架,选取需要的包会自动添加到pom,填写group artifact,分别生成user,和movie两个骨架


用ide 导入项目,在user下新建目录和java文件如下,user实体可以自己写建个mysql的表对应即可,DAO可以用jpa或者mybatis,这里用jpa

controller 文件如下

@RestController
public class UserController {
    @Autowired
    UserDAO userDAO;
//    @Autowired
//    EurekaDiscoveryClient discoveryClient;
//    @Autowired
//    DiscoveryClient discoveryClient;

    @GetMapping("/user/{id}")
    public User find(@PathVariable int id){

        return userDAO.findOne(id);
    }
}

这是user的properties

user实体
pom文件又添加了eureka,和cloud



    4.0.0

    com.security.cloud
    microservice-simple-provider-user
    0.0.1-SNAPSHOT
    jar

    microservice-simple-provider-user
    Demo project for Spring Boot

    
        org.springframework.boot
        spring-boot-starter-parent
        1.4.7.RELEASE
         
    

    
        UTF-8
        UTF-8
        1.8
        Camden.SR7
    

    

        
            org.springframework.cloud
            spring-cloud-starter-eureka
        
        
            org.springframework.boot
            spring-boot-starter-data-jpa
        
        
            org.mybatis.spring.boot
            mybatis-spring-boot-starter
            1.2.2
        
        
            org.springframework.boot
            spring-boot-starter-web
        

        
            org.springframework.boot
            spring-boot-devtools
            runtime
        
        
            com.h2database
            h2
            runtime
        
        
            mysql
            mysql-connector-java
            runtime
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
        
        
            org.springframework.boot
            spring-boot-starter-actuator
        
    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    

    
        
            
                org.springframework.cloud
                spring-cloud-dependencies
                ${spring-cloud.version}
                pom
                import
            
        
    



这是movie,user实体和user模块的一致,properties 配置端口不同就行

config主要配置了 restTemplate的bean

@Configuration
public class Config {

    @Bean

    public RestTemplate restTemplate(){
        return new RestTemplate();
    }
}

movie controller,从user模块获取user

@RestController
public class NewsController {
    @Autowired
    RestTemplate restTemplate;

    @RequestMapping("/newsUser/{id}")
    public User find(@PathVariable int id){
        return restTemplate.getForObject("http://localhost:8080/user/"+id,User.class);
    }
}

movie pom文件



    4.0.0

    com.security.cloud
    microservice-simple-consumer-movie
    0.0.1-SNAPSHOT
    jar

    microservice-simple-consumer-movie
    Demo project for Spring Boot

    
        org.springframework.boot
        spring-boot-starter-parent
        1.4.7.RELEASE
         
    

    
        UTF-8
        UTF-8
        1.8
        Camden.SR7
    

    
        
            org.springframework.cloud
            spring-cloud-starter-eureka
        
        
            org.springframework.boot
            spring-boot-starter-data-jpa
        
        
            org.mybatis.spring.boot
            mybatis-spring-boot-starter
            1.2.2
        
        
            org.springframework.boot
            spring-boot-starter-web
        

        
            org.springframework.boot
            spring-boot-devtools
            runtime
        
        
            com.h2database
            h2
            runtime
        
        
            mysql
            mysql-connector-java
            runtime
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
        
    

    
        
            
                org.springframework.cloud
                spring-cloud-dependencies
                ${spring-cloud.version}
                pom
                import
            
        
    
    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    



分别启动user,movie 模块打开controller的url以及自己设的端口

消费者微服务成功调用提供者微服务

接下来设置 服务注册中心eureka,实际情况中,微服务ip和端口都为动态,需要服务注册中心来管理,如dubbo通过zookeeper管理

新建一个eureka项目,

pom文件



    4.0.0

    com.security.cloud
    microservice-discovery-eureka
    0.0.1-SNAPSHOT
    jar

    microservice-discovery-eureka
    Demo project for Spring Boot

    
        org.springframework.boot
        spring-boot-starter-parent
        1.4.7.RELEASE
         
    

    
        UTF-8
        UTF-8
        1.8
        Camden.SR7
    


    
        
            org.springframework.boot
            spring-boot-starter-security
        
        
            org.springframework.cloud
            spring-cloud-starter-eureka
        
        
            org.springframework.cloud
            spring-cloud-starter-eureka-server
        
        
            org.springframework.boot
            spring-boot-starter-data-jpa
        
        
            org.mybatis.spring.boot
            mybatis-spring-boot-starter
            1.2.2
        
        
            org.springframework.boot
            spring-boot-starter-web
        

        
            org.springframework.boot
            spring-boot-devtools
            runtime
        
        
            mysql
            mysql-connector-java
            runtime
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
        
    

    
        
            
                org.springframework.cloud
                spring-cloud-dependencies
                ${spring-cloud.version}
                pom
                import
            
        
    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    



application:

@SpringBootApplication
@EnableEurekaServer
public class MicroserviceDiscoveryEurekaApplication {

    public static void main(String[] args) {
        SpringApplication.run(MicroserviceDiscoveryEurekaApplication.class, args);
    }
}

properties:

server.port=8761
#服务注册中心实例的主机名

eureka.instance.hostname=localhost

#是否向服务注册中心注册自己

eureka.client.register-with-eureka=false

#是否检索服务

eureka.client.fetch-registry=false

#服务注册中心的配置内容,指定服务注册中心的位置

eureka.client.serviceUrl.defaultZone=http://user:pass@${eureka.instance.hostname}:${server.port}/eureka/
#eureka.instance.instance-id=${spring.application.name}:${spring.application.instance_id:${server.port}}

security.basic.enabled=true
security.user.name=user
security.user.password=pass

启动eureka 的application

然后在user微服务的application下添加注解@EnableEurekaClient,指名是EurekaClient,eureka的客户端

@SpringBootApplication
@EnableEurekaClient
public class MicroserviceSimpleProviderUserApplication {
    public static void main(String[] args) {
        SpringApplication.run(MicroserviceSimpleProviderUserApplication.class, args);
    }
}

启动user微服务,最后在eureka的url可以看到,因为properties设置了security为true,要输入在配置里设置的用户名密码才能登陆进去,看到注册的微服务

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/33215.html

相关文章

  • java-study-springcloud-网络资料-01-微服务是什么

    摘要:本系列网络资料资料来源于网络,相关学习微服务与微服务架构定义理解单一应用程序划分为一组小的服务,每个服务有自己的进程。 本系列(java-study-springcloud-网络资料)资料来源于网络,springcloud相关学习 1、微服务与微服务架构 定义:https://martinfowler.com/arti... showImg(https://segmentfault.c...

    JerryZou 评论0 收藏0
  • 整理一下学习微服务springboot+springcloud+vue以来用到的好的博客

    摘要:调用百度实现图像识别使用渲染导出的制作的超级炫酷的三维模型一个代码库本人本人浏览器调试及所有错误代码整合千峰超级好用的各种开发自学文档这是它对应的学习视频使用教程详细虚拟机安装系统详解版网易开源镜像站在线数据互转使 1.Java调用百度API实现图像识别 2.使用Three.js渲染Sketchup导出的dae 3.three.js制作的超级炫酷的三维模型 4.three.js - 一...

    gitmilk 评论0 收藏0
  • 整理一下学习微服务springboot+springcloud+vue以来用到的好的博客

    摘要:调用百度实现图像识别使用渲染导出的制作的超级炫酷的三维模型一个代码库本人本人浏览器调试及所有错误代码整合千峰超级好用的各种开发自学文档这是它对应的学习视频使用教程详细虚拟机安装系统详解版网易开源镜像站在线数据互转使 1.Java调用百度API实现图像识别 2.使用Three.js渲染Sketchup导出的dae 3.three.js制作的超级炫酷的三维模型 4.three.js - 一...

    bluesky 评论0 收藏0
  • 整理一下学习微服务springboot+springcloud+vue以来用到的好的博客

    摘要:调用百度实现图像识别使用渲染导出的制作的超级炫酷的三维模型一个代码库本人本人浏览器调试及所有错误代码整合千峰超级好用的各种开发自学文档这是它对应的学习视频使用教程详细虚拟机安装系统详解版网易开源镜像站在线数据互转使 1.Java调用百度API实现图像识别 2.使用Three.js渲染Sketchup导出的dae 3.three.js制作的超级炫酷的三维模型 4.three.js - 一...

    enrecul101 评论0 收藏0
  • Springcloud-nacos实现配置和注册中心

    摘要:实现配置和注册中心最近,阿里开源的比较火,可以和和共用,对升级到非常的方便。只需要添加依赖,使用配置注册中心地址即可。配置不生效,没有使用注解刷新配置分清注册中心和配置中心是两个概念,需要配置两个地址学会看源码,看维基。 Springcloud-nacos实现配置和注册中心 最近,阿里开源的nacos比较火,可以和springcloud和dubbo共用,对dubbo升级到springc...

    whinc 评论0 收藏0
  • springCloud Finchley 微服务架构从入门到精通【十一】Tomcat 部署项目

    摘要:在基于老版本的中使用部署项目是比较麻烦的,你可能需要在中手动排除来部署到线上环境而排除后中又无法使用类启动工程。在新版本的中,开发者们解决了这个问题,现在使用部署项目是非常简单的,只需要简单两步就可以保证本地和线上同时可用。   在基于老版本的springcloud中使用tomcat部署项目是比较麻烦的,你可能需要在pom.xml中手动排除tomcat来部署到线上环境,而排除tomca...

    Rocture 评论0 收藏0

发表评论

0条评论

harryhappy

|高级讲师

TA的文章

阅读更多
最新活动
阅读需要支付1元查看
<